<?php
		include "../common/db.inc";
		$error = '';
		//xoa
		if (isset($_GET["deletedId"]))
		{
			$mysqli = new mysqli($host, $username, $password,$dbname);
			$mysqli->set_charset('utf8');	
			/* check connection */
			if (mysqli_connect_errno()) {
			    printf("Connect failed: %s\n", mysqli_connect_error());
			    exit();
			$mysqli->close();
			}
			
			$id = $_REQUEST["deletedId"];
			$query = "call sp_GetEbookByType('$id')";
			$result = $mysqli->query($query);
			$mysqli->close();
			if($result)
			{
				if(mysqli_num_rows($result) != 0)
				{
					$error = "Đã có ebook thuộc loại này. Không thể xóa loại sách này";	
				}
				else
				{
					$mysqli = new mysqli($host, $username, $password,$dbname);
					$query = "call sp_DeleteEbookType('$id')";
					$mysqli->query($query);
					$mysqli->close();
				}
			}			
		}
		
		//them
		if (isset($_GET["addedName"]))
		{
			$mysqli = new mysqli($host, $username, $password,$dbname);
			$mysqli->set_charset('utf8');	
			/* check connection */
			if (mysqli_connect_errno()) 
			{
			    printf("Connect failed: %s\n", mysqli_connect_error());
			    exit();
				$mysqli->close();
			}
			$name = $_REQUEST["addedName"];
			$query = ("call sp_AddEbookType('$name')");
			$mysqli->query($query);
			$mysqli->close();
		}
		
		//cap nhat
		if (isset($_GET["updatedId"]))
		{
			$mysqli = new mysqli($host, $username, $password,$dbname);
			$mysqli->set_charset('utf8');	
			/* check connection */
			if (mysqli_connect_errno()) 
			{
			    printf("Connect failed: %s\n", mysqli_connect_error());
			    exit();
				$mysqli->close();
			}
			$id = $_REQUEST["updatedId"];
			$name = $_REQUEST["name"];
			$query = ("call sp_UpdateEbookType('$id', '$name')");
			$mysqli->query($query);
			$mysqli->close();
		}

		$mysqli = new mysqli($host, $username, $password,$dbname);
		$mysqli->set_charset('utf8');	
		/* check connection */
		if (mysqli_connect_errno()) {
		    printf("Connect failed: %s\n", mysqli_connect_error());
		    exit();
		}
		
		$query = ("call sp_GetAllEbookType()");
		
		if ($result = $mysqli->query($query)) 
		{
			echo "<div id='divResult'>";
			if($error != '')
				echo "<div style='text-align: center; color: red'>$error</div><br/>";
			echo "<table border='1' align = 'center'>";
			echo "<tr style='background-color:#AACCEE' >";
			echo "<th>ID</th>";
			echo "<th>Tên</th>";
			echo "<td>&nbsp;</td></tr>";
			$i = 0;
    		while ($row = $result->fetch_row()) 
			{
				$id = $row[0];
				$name = $row[1];
				$i = 1 - $i;
				if($i == 0)
					echo "<tr style='background-color:#FFFFE1'>";
				else 
					echo "<tr style='background-color:#E5E5E5'>";
				echo"<td style='text-align:center;'>$id</td>";
				echo"<td>$name</td>";
				echo "<td><input type=\"button\" value=\"Xoá\" onclick=\"XuLyXoa('$id', 3);\"/>";
				echo "<input type=\"button\" value=\"Cập nhật\" onclick='btnCapnhat_Click(this, 3)' /></td>";
				echo"</tr>";
				
			}
		}
		echo"<tr>";
		echo"<td width='40px'></td>";
		echo "<td><input type=\"text\" id =\"txtName\" size='30'/></td>";
		echo "<td><input type=\"button\" value=\"Thêm\" onclick=\"XuLyThem(3);\"/></td>";
		echo"</tr>";
	 $result->close();
echo "</table>";	
echo "</div>";

$mysqli->close();
		
?>
